package com.fupeng.leetCode;

import java.util.Arrays;

/**
 * Given a list of non negative integers, arrange them such 
 * that they form the largest number.
	For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

	Note: The result may be very large, 
	so you need to return a string instead of an integer.
 * @author lecky
 *
 */
public class BiggestNum {

	public static void main(String[] args) {
		int[] T = {3,30,34,5,9};
		System.out.println(solution(T));
	}
	public static String solution(int[] T){
		String result = "";
		String[] str = new String[T.length];
		for(int i=0;i<T.length;i++)
			str[i] = String.valueOf(T[i]);
		
		int index= 0;
		String temp=null;
		for(int i=0;i<str.length;i++){
			index = i;
			for(int j=i+1;j<str.length;j++){
				if((str[index]+str[j]).compareTo(str[j]+str[index])<0)
					index=j;
			}
			temp = str[i];
			str[i] = str[index];
			str[index] = temp;
		}
		for(int i=0;i<str.length;i++)
			result = result.concat(str[i]);
		return result;
		
	}
}
